////
////  WCNForgotPwdVC.m
////  WCN
////
////  Created by 杨鑫 on 15/5/30.
////  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
////
//



#import "HELForgotPwdVC.h"
//
#import "WCTimer.h"
#import "NSString+Utilities.h"
static __weak WCTimer *timerFgt;

@interface HELForgotPwdVC () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
- (IBAction)togglePwdBtn:(id)sender;
//
@end
//
@implementation HELForgotPwdVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"忘记密码";
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleView:@"找回密码"];
    // Do any additional setup after loading the view from its nib.
    self.pwdTextField.delegate = self;
    self.codeBtn.layer.borderColor = kBtnColor.CGColor;
    self.codeBtn.layer.cornerRadius = 5.0;
    self.codeBtn.layer.borderWidth = 0.5;
//    timerFgt.securityCode = self.codeBtn;
//    [self setNavToBlue];
}

#pragma mark - 倒计时

- (void)countDown
{
   WCTimer *timer = [[WCTimer alloc] init];
    __block __weak typeof(WCTimer *)weakTimer = timer;
    timer.count = 60;
    timer.securityCode = self.codeBtn;
    timer.timeEnd = ^{
        weakTimer = nil;
    };
    [timer start:1];
    
    timerFgt = timer;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

#pragma mark - Notification

- (void)keyboardWillHide:(NSNotification *)note
{
    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:7<<16
                     animations:^{
                         self.view.transform = CGAffineTransformIdentity;
                     } completion:NULL];
}

#pragma mark - Nib Target

//发送验证码
- (IBAction)codeBtnTap:(id)sender {
    NSString *mobile = self.phoneTextField.text;
    if ([mobile isEmpty]) {
        alertMessage(@"手机号不能为空");
        return;
    }
    if ([mobile isMobile]) {
    
        self.codeBtn.enabled = NO;
        cwqHud(self.view);
    
        [[HELNetworkManager sharedMg] POST:helVerificationCodeUrl parameters:@{@"mobile":mobile} success:^(id responseObject) {

            //倒计时
            if ([responseObject[@"root"] isEqualToString:@"success"]) {
        
                [self countDown];
                cwqHudSuccess(hud);
            }else {
                cwqHudRequestError(responseObject[@"exception"]);
            }
            self.codeBtn.enabled = YES;
        } failure:^(NSString *err) {
             self.codeBtn.enabled = YES;
            cwqHudRequestError(err);
        
        }];
   
    }else {
    
        alertMessage(@"手机号错误");
    }
    

}


- (IBAction)bgTouchDown:(id)sender {
    [self.view endEditing:YES];
}

- (IBAction)togglePwdBtn:(id)sender {
    UIButton *btn = sender;
    
    if (btn.selected) {
        [btn setSelected:NO];
        
        self.pwdTextField.secureTextEntry = YES;
    } else {
        [btn setSelected:YES];
        
        NSString *passwordStr = self.pwdTextField.text;
        //        self.pwdTextField.text = @"";
        self.pwdTextField.secureTextEntry = NO;
        self.pwdTextField.text = passwordStr;
    }
}

- (IBAction)doSubmit:(id)sender {
    NSString *account = self.phoneTextField.text, *code = self.codeTextField.text, *password = self.pwdTextField.text;
    
    if ([account isEmpty]) {
        alertMessage(@"请输入手机号");
        return;
    }
    
    if ([code isEmpty]) {
        alertMessage(@"请输入验证码");
        return;
    }
    
    if ([password isEmpty]) {
        alertMessage(@"请输入密码");
        return;
    }
    
    if ([password hasSpace]) {
        alertMessage(@"密码不能有空格");
        return;
    }
    
    if (![password isPassword]) {
        alertMessage(@"密码长度为6-16位");
        return;
    }
    

    cwqHud(self.view);
    weakSelf_define;
//    newPwd 新密码
//    code  验证码
//    mobile   手机号码
    NSDictionary *param = @{@"newPwd":password, @"code":code, @"mobile":account};
    [[HELNetworkManager sharedMg] POST:helForgetPasswordUrl parameters:param success:^(id responseObject) {
        if (responseObject[@"exception"]) {
            cwqHudRequestError(responseObject[@"exception"]);
        }else {

            cwqHudSuccess(hud);
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        
    } failure:^(NSString *err) {
            cwqHudRequestError(err);
    }];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    UIView *mustShow;
    if (textField.tag == 0) {
        mustShow = self.codeTextField;
    } else if (textField.tag == 2) {
        mustShow = self.pwdTextField;
    }
    
    CGRect rect = [mustShow convertRect:mustShow.bounds toView:self.view];
    CGFloat y = self.view.frame.size.height - rect.origin.y - rect.size.height - kKeyboardHeight;
    
    #warning 为了配合键盘弹出的动画，使用7<<16.
    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:7<<16
                     animations:^{
                         self.view.transform = CGAffineTransformMakeTranslation(0, y>=0?0:y-10);
                     } completion:NULL];
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == 2) {
        [textField resignFirstResponder];
        [self doSubmit:nil];
        return YES;
    }
    
    return NO;
}

@end
